package com.cm.module.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cm.constants.CommonConstant;
import com.cm.module.system.entity.SysUser;
import com.cm.module.system.service.ISysUserService;
import com.cm.restful.QueryRequestBody;
import com.cm.restful.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

 /**
 * @Description: 用户信息表
 * @Author: jeecg-boot
 * @Date:   2021-08-09
 * @Version: V1.0
 */
@Api(tags="用户信息表")
@RestController
@RequestMapping("sys/sysUser")
@Slf4j
public class SysUserController  {
	@Autowired
	private ISysUserService sysUserService;

	/**
	 * 分页列表查询
	 *
	 * @return
	 */
	//@AutoLog(value = "用户信息表-分页列表查询")
	@ApiOperation(value="用户信息表-分页列表查询", notes="用户信息表-分页列表查询")
	@GetMapping(value = "/list")
	public ResponseBody<IPage<SysUser>> findUserInfoList(QueryRequestBody<SysUser> params) {
		ResponseBody<IPage<SysUser>> responseBody = new ResponseBody<IPage<SysUser>>();
		try {
			responseBody.setData(sysUserService.findUserInfoList(params.getParams(),params.getQueryRequest()));
		}catch (Exception e){
			log.error("findUserInfoList -=- {}", e);
			responseBody.error500(CommonConstant.ERROR_BAD);
		}
		return responseBody.OK(CommonConstant.SELECT_SUCCESS);
	}
	
	/**
	 *   添加
	 *
	 * @param sysUser
	 * @return
	 */
	//@AutoLog(value = "用户信息表-添加")
	@ApiOperation(value="用户信息表-添加", notes="用户信息表-添加")
	@PostMapping(value = "/add")
	public ResponseBody add(@RequestBody SysUser sysUser) {
		sysUserService.save(sysUser);
		return new ResponseBody().OK(CommonConstant.INSERT_SUCCESS);
	}
	
	/**
	 *  编辑
	 *
	 * @param sysUser
	 * @return
	 */
	//@AutoLog(value = "用户信息表-编辑")
	@ApiOperation(value="用户信息表-编辑", notes="用户信息表-编辑")
	@PutMapping(value = "/edit")
	public ResponseBody<?> edit(@RequestBody SysUser sysUser) {
		sysUserService.updateById(sysUser);
		return new ResponseBody().OK(CommonConstant.UPDATE_SUCCESS);
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户信息表-通过id删除")
	@ApiOperation(value="用户信息表-通过id删除", notes="用户信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public ResponseBody<?> delete(@RequestParam(name="id",required=true) String id) {
		sysUserService.removeById(id);
		return new ResponseBody().OK(CommonConstant.DELETE_SUCCESS);
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	//@AutoLog(value = "用户信息表-批量删除")
	@ApiOperation(value="用户信息表-批量删除", notes="用户信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public ResponseBody<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysUserService.removeByIds(Arrays.asList(ids.split(",")));
		return new ResponseBody().OK(CommonConstant.DELETE_IDS_SUCCESS);
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户信息表-通过id查询")
	@ApiOperation(value="用户信息表-通过id查询", notes="用户信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public ResponseBody<SysUser> queryById(@RequestParam(name="id",required=true) String id) {
		ResponseBody<SysUser> responseBody = new ResponseBody<SysUser>();
		SysUser sysUser = sysUserService.getById(id);
		if(sysUser==null) {
			return new ResponseBody().error500(CommonConstant.FIND_NONE_MESSAGE);
		}
		responseBody.setData(sysUser);
		return new ResponseBody().OK(CommonConstant.SELECT_SUCCESS);
	}


}
